﻿using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.UI;

public class PictureItems
{
    private List<string> Paths;

    private List<string> PathPools;

    public PictureItems()
    {
        Paths = new List<string>();
    }

    /// <summary>
    /// 随机获取图片  
    /// </summary>
    public string GetPictureRandom
    {
        get
        {
            if (Paths.Count > 0)
            {
                int index = Random.Range(0, Paths.Count-1);
                string path = Paths[index];
                return path;
            }
            else
            {
                return string.Empty;
            }
        }
    }

    public void AddPath(string Path)
    {
        Paths.Add(Path);
    }
}

/// <summary>
/// 管理加载图片
/// </summary>
public class PictureCenter : MonoBehaviour
{
    public static PictureCenter Instance;

    public PictureItems pictureItems = new PictureItems();
    private void Awake()
    {
        Instance = this;
        ForeachFile(Application.streamingAssetsPath);
    }

    //遍历文件夹下所有可用文件的文件
    public void ForeachFile(string filePathByForeach)
    {
        DirectoryInfo theFolder = new DirectoryInfo(filePathByForeach);
        DirectoryInfo[] dirInfo = theFolder.GetDirectories();//获取所在目录的文件夹
        FileInfo[] file = theFolder.GetFiles();//获取所在目录的文件
        foreach (FileInfo fileItem in file) //遍历文件
        {
            string[] strtemp = fileItem.Name.Split('.');
            if (strtemp.Length >= 2)
            {
                string[] type = fileItem.FullName.Split('.');
                if (type.Length >= 2)
                {
                    //筛选出可用文件
                    for (int i = 0; i < DataModel.PictureType.Length; i++)
                    {
                        if (type[type.Length - 1] == DataModel.PictureType[i])
                        {
                            pictureItems.AddPath(fileItem.FullName);
                        }
                    }
                }
            }
        }
        //遍历文件夹
        foreach (DirectoryInfo NextFolder in dirInfo)
        {
            ForeachFile(NextFolder.FullName);
        }
    }

}
